#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 800

/**********************
 *  STATIC VARIABLES
 **********************/
lv_obj_t * obj ;
lv_obj_t * wifi_text ;
lv_obj_t * wifi_start_scan ;

lv_obj_t * wifi_state_text ;
lv_obj_t * wifi_state ;

lv_style_t label_style ;
lv_style_t button_style_release ;
lv_style_t button_style_press ;
lv_style_t wifi_list_style ;


lv_obj_t * wifi_scan_list ;
lv_obj_t* wifi_list_btn;
lv_obj_t* wifi_list_label;

pthread_t wifi_manage;
pthread_cond_t m_Cond;
pthread_mutex_t MsgMutex ;
/**********************
 *      MACROS
 **********************/
#include <stdio.h>
/**********************
 *   GLOBAL FUNCTIONS
 **********************/
 #if 0
static void switch_event(lv_event_t * e)
{
    static uint8_t status ;
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_VALUE_CHANGED)
    {
        switch(status)
        {
        case 0:
			lv_obj_add_state(wifi_start_scan, LV_STATE_DISABLED); 
			lv_label_set_text(wifi_state,"Scanning..");
			pthread_mutex_lock(&MsgMutex);
			pthread_cond_signal(&m_Cond);
            pthread_mutex_unlock(&MsgMutex);
            status = 1 ;
            break ;
        case 1:
			printf("Touch close wifi scan.........\n");
			lv_label_set_text(wifi_state,"Disconnect");
			printf("lv_label_set_text ok.........\n");
			lv_obj_del(wifi_scan_list);
			wifi_scan_list = NULL ;
			wifi_list_label = NULL ;
			wifi_list_btn = NULL ;
			printf("var is null!...\n");
            status = 0 ;
            break ;
        default:
            break ;
        }
    }
}
 
void wifi_list_scan(void)
{
	FILE* fp;
	int ret = 0;
	char str[100];
	char *q = NULL;
	char buf[255] = {0};
	char command[128] = "/config/wifi/iwlist wlan0 scan";
	if(NULL != wifi_scan_list || NULL != wifi_list_btn 
		|| NULL != wifi_list_label)
		return ;
    wifi_scan_list = lv_list_create(obj);
	lv_obj_set_pos(wifi_scan_list,0,160);
    lv_obj_set_size(wifi_scan_list,431,481);
	lv_obj_set_style_pad_all(wifi_scan_list,10,0);
    lv_obj_set_style_pad_row(wifi_scan_list, 10, 0);
    lv_obj_set_style_bg_color(wifi_scan_list,lv_color_hex(0xE3EAF4),0);
    lv_obj_set_style_border_color(wifi_scan_list,lv_color_hex(0x3E6094),0);
    lv_obj_set_style_border_opa(wifi_scan_list,LV_OPA_30,0);
	if((fp = popen(command, "r")) != NULL)
	{
		while(fgets(buf, sizeof(buf), fp) != NULL)
		{
			memset(str,0,100);
			q = strstr(buf, "ESSID:");
			if (q != NULL)
			{
				printf("start strstr..ok..%s\n",q);
				sscanf(q, "ESSID:\"%[^\"]\"", str);
				q = NULL;
				printf("Wifi name:%s\n",str);
				wifi_list_btn = lv_btn_create(wifi_scan_list); 
				lv_obj_center(wifi_list_btn);
				lv_obj_set_width(wifi_list_btn, lv_pct(100));
				lv_obj_set_height(wifi_list_btn, lv_pct(12));
				lv_obj_add_style(wifi_list_btn,&button_style_release,LV_STATE_DEFAULT);
				lv_obj_add_style(wifi_list_btn,&button_style_press,LV_STATE_PRESSED);
				wifi_list_label = lv_label_create(wifi_list_btn);
				lv_label_set_text_fmt(wifi_list_label, "%s", str);
				lv_obj_set_align(wifi_list_label,LV_TEXT_ALIGN_AUTO);
				lv_label_set_text(wifi_state,"Wifi Scan ok");
			}
		}
		pclose(fp);
	}
	lv_obj_clear_state(wifi_start_scan, LV_STATE_DISABLED);
}
#endif
void wifi_list_scan_test(void)
{
    wifi_scan_list = lv_list_create(lv_layer_top());
    lv_obj_set_size(wifi_scan_list,431,481);
    lv_obj_set_pos(wifi_scan_list,20,160);
	lv_obj_add_style(wifi_scan_list,&wifi_list_style,LV_STATE_DEFAULT);
}

static void switch_event(lv_event_t * e)
{
    static uint8_t status ;
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_VALUE_CHANGED)
    {
        switch(status)
        {
        case 0:
			pthread_cond_signal(&m_Cond);
            status = 1 ;
            break ;
        case 1:
            lv_obj_del(wifi_scan_list);
			wifi_scan_list = NULL ;
            status = 0 ;
            break ;
        default:
            break ;
        }
    }
}

void Wifi_function_handler(void *arg)
{
	while(1)
	{
		pthread_mutex_lock(&MsgMutex);
		pthread_cond_wait(&m_Cond, &MsgMutex);
		pthread_mutex_unlock(&MsgMutex);
		wifi_list_scan_test();
		usleep(5000);
	}
}

void lvgl_test_demo(void)
{
	printf("1\n");
    lv_style_init(&label_style);
    lv_style_set_bg_color(&label_style,lv_color_hex(0xE3EAF4));
    lv_style_set_text_color(&label_style,lv_color_hex(0x3E6094));
    lv_style_set_border_color(&label_style,lv_color_hex(0xE3EAF4));
	printf("2\n");
    lv_style_init(&button_style_release);
    //设置圆角
    lv_style_set_radius(&button_style_release,2);
    //设置边框宽度
    lv_style_set_border_width(&button_style_release, 2);
    //设置背景颜色
    lv_style_set_bg_color(&button_style_release,lv_color_hex(0xE3EAF4));
    //设置文本颜色
    lv_style_set_text_color(&button_style_release,lv_color_hex(0x3E6094));
    //设置边框颜色
    lv_style_set_border_color(&button_style_release, lv_color_hex(0x3E6094));
    //设置背景透明度
    lv_style_set_bg_opa(&button_style_release, LV_OPA_COVER);
	printf("3\n");
    lv_style_init(&button_style_press);
    //设置圆角
    lv_style_set_radius(&button_style_press,2);
    //设置边框宽度
    lv_style_set_border_width(&button_style_press, 2);
    //设置背景颜色
    lv_style_set_bg_color(&button_style_press,lv_color_hex(0x3E6094));
    //设置文本颜色
    lv_style_set_text_color(&button_style_press,lv_color_hex(0x3E6094));
    //设置边框颜色
    lv_style_set_border_color(&button_style_press, lv_color_hex(0x3E6094));
    //设置背景透明度
    lv_style_set_bg_opa(&button_style_press,LV_OPA_30);
	printf("4\n");
	lv_style_init(&wifi_list_style);
	lv_style_set_pad_all(&wifi_list_style,10);
    lv_style_set_pad_row(&wifi_list_style, 10);
    lv_style_set_bg_color(&wifi_list_style,lv_color_hex(0xE3EAF4));
    lv_style_set_border_color(&wifi_list_style,lv_color_hex(0x3E6094));
    lv_style_set_border_opa(&wifi_list_style,LV_OPA_30);

	printf("5\n");
    wifi_text = lv_label_create(obj);
    lv_obj_set_size(wifi_text,359,51);
    lv_obj_set_pos(wifi_text,15,51);
    lv_obj_add_style(wifi_text,&label_style,0);
    lv_label_set_text(wifi_text,"WIFI");
	printf("6\n");
    wifi_start_scan = lv_switch_create(obj);
	printf("6.1\n");
	lv_obj_set_size(wifi_start_scan,64,32);
	printf("6.2\n");
    lv_obj_align_to(wifi_start_scan,wifi_text,LV_ALIGN_RIGHT_MID,40,-12);
	printf("6.3\n");
    lv_obj_add_event_cb(wifi_start_scan,switch_event,LV_EVENT_VALUE_CHANGED,NULL);
	printf("7\n");
    wifi_state_text = lv_label_create(obj);
    lv_obj_set_size(wifi_state_text,359,51);
    lv_obj_align_to(wifi_state_text,wifi_text,LV_ALIGN_LEFT_MID,0,60);
    lv_obj_add_style(wifi_state_text,&label_style,0);
    lv_label_set_text(wifi_state_text,"WiFi State");
	printf("8\n");
    wifi_state = lv_label_create(obj);
    lv_obj_set_size(wifi_state,359,51);
    lv_obj_align_to(wifi_state,wifi_state_text,LV_ALIGN_RIGHT_MID,280,0);
    lv_obj_add_style(wifi_state,&label_style,0);
    lv_label_set_text(wifi_state,"Disconnect");
	printf("9\n");
}

int main(void)
{
    /*LittlevGL init*/
    lv_init();

    /*Linux frame buffer device init*/
    fbdev_init();

	//双全缓冲刷屏
	static lv_disp_draw_buf_t draw_buf_dsc_3;
    static lv_color_t buf_3_1[SCREEN_WIDTH * SCREEN_HEIGHT];            /*A screen sized buffer*/
    static lv_color_t buf_3_2[SCREEN_WIDTH * SCREEN_HEIGHT];            /*Another screen sized buffer*/
    lv_disp_draw_buf_init(&draw_buf_dsc_3, buf_3_1, buf_3_2, SCREEN_WIDTH * SCREEN_HEIGHT);   /*Initialize the display buffer*/
	
    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &draw_buf_dsc_3;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = SCREEN_WIDTH;
    disp_drv.ver_res    = SCREEN_HEIGHT;
    lv_disp_drv_register(&disp_drv);

    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);

	
    /*Set a cursor for the mouse*/
    LV_IMG_DECLARE(mouse_cursor_icon)
    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
    lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/

	//system("/config/wifi/wpa_supplicant -Dnl80211 -i wlan0 -c /appconfigs/wpa_supplicant.conf & sleep 5");
    obj = lv_obj_create(lv_scr_act());
    lv_obj_set_size(obj,SCREEN_WIDTH,SCREEN_HEIGHT);
    lv_obj_center(obj);
    lv_obj_set_style_bg_color(obj,  lv_color_hex(0xE3EAF4), LV_STATE_DEFAULT);
    //禁止滑动
    lv_obj_clear_flag(obj,LV_OBJ_FLAG_SCROLLABLE);
    lvgl_test_demo();
	
	pthread_mutex_init (&MsgMutex, NULL);
	pthread_cond_init(&m_Cond, NULL);
	pthread_create(&wifi_manage, NULL, Wifi_function_handler,NULL);
    pthread_detach(wifi_manage);
	
    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) 
	{
        lv_timer_handler();
        usleep(5000);
    }
    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
